import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;


/** 
 * Thread for handling the transmission of the current Link Length Table for the peer
 * 
 **/
public class GRIPPeerThread extends Thread {
	private static Socket GRIPSocket;
	private static PGPeer peer;
	private static Point pos;
	private BufferedReader in = null;// 
	private DataOutputStream out = null;//
	
	public GRIPPeerThread(BufferedReader in2, PGPeer peer, Point pos){
		this.in = in2;
		this.peer = peer;
		this.pos = pos;
		
	}
	
	public void run(){
		char[] num = new char[4];
		try {
			//BufferedReader in = new BufferedReader(new InputStreamReader(GRIPSocket.getInputStream()));		
			//for(int i=0; i<4; i++){			
			//	in.read(num, 0, 4);			
			//}			
			in.read(num, 0, 4);
			
			int numEntries = Integer.parseInt(new String(num), 16);
			
			peer.showGRIPTable("Before Updates:");
			
			// Read in each entry and update the table
			for(int i=0; i<numEntries; i++){
				// Get the x coordinate
				in.read(num, 0, 4);
				int x = Integer.parseInt(new String(num), 16);
				
				// Get the y coordinate
				in.read(num, 0, 4);
				int y = Integer.parseInt(new String(num), 16);
				
				// Get the length
				in.read(num, 0, 4);
				int distance = Integer.parseInt(new String(num), 16);
				
				peer.setGRIPTable(new Point(x, y), distance);
			}
			
			peer.showGRIPTable("After Updates:");
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
	}
	
}
